const BASE_URL = 'http://api.dekunlu.top'
export const myRequest = (options = {}) => {
	//加载loading
	uni.showLoading({
		title: '加载中',
	})
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${BASE_URL}${options.url}`,
			method: options.method || 'GET',
			data: options.data || {},
			header: {
				"Content-Type": "application/json",
				"Authorization": uni.getStorageSync('token') || ''
			},
			success: ({
				data
			}) => {
				uni.hideLoading()
				if (data.code === 400) {
					return uni.showToast({
						title: "服务器错误",
					})
				}else if(data.code !== 200){
					return uni.showToast({
						title: "获取失败",
					})
				}
				return resolve(data.data);
			},
			fail: (err) => {
				uni.hideLoading()
				uni.showToast({
					title: "接口申请失败",
					icon: "error"
				});
				return reject(err)
			},
			//请求结束之后，执行的回调函数（成功或失败都会执行）
			complete: () => {
				//隐藏loading
				setTimeout(() => {
					uni.hideToast()
				}, 1000)
			}
		});
	})
}
